package com.example.szh.mvp.view.user;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.blankj.utilcode.util.LogUtils;
import com.example.szh.R;
import com.example.szh.db.DBManager;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.UserEntity;
import com.example.szh.mvp.contract.UpDataUserContract;
import com.example.szh.mvp.di.DaggerUpDataComponent;
import com.example.szh.mvp.di.UpDataUserModues;
import com.example.szh.mvp.presenter.UpDataUserPresenter;
import com.example.szh.nextwork.view.BaseActivity;
import com.example.szh.utils.LoadImage;

import java.io.File;
import java.util.List;

public class UserActivity extends BaseActivity<UpDataUserPresenter> implements View.OnClickListener, UpDataUserContract.IUpDataUserView {

    private ImageView userBackBnt;
    private ImageView userToUserImg;
    private TextView userToUserName;
    private TextView sex;
    private LinearLayout sexLinear;
    private Button sexNull;
    private Button sexBody;
    private Button sexGirl;
    private Button cancel1;
    private LinearLayout pzLinear;
    private Button photograph;
    private Button mapDepot;
    private Button cancel2;
    private String path;
    private LinearLayout userLinear;


    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.umeng_back) {

        } else if (v.getId() == R.id.user_toUserImg) {

        } else if (v.getId() == R.id.user_toUserName) {
            Intent intent = new Intent(this, NickNameActivity.class);
            startActivityForResult(intent, 101);
        } else if (v.getId() == R.id.sex) {
            sexLinear.setVisibility(View.VISIBLE);
        }else if (v.getId()==R.id.sex_body){
            sex.setText("男");
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();

                if (list.size() != 0) {
                    list.get(0).setUser_sex("1");
                    list.get(0).setUser_integral(1);
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
            sexLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.sex_girl){
            sex.setText("女");
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();

                if (list.get(0) != null) {
                    list.get(0).setUser_sex("0");
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
            sexLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.sex_null){
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                if (list.get(0) != null) {
                    list.get(0).setUser_sex("0");
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));
                sex.setText("保密");
            } catch (Exception e) {
                e.printStackTrace();
            }
            sexLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.cancel1){
            sexLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.cancel2){
            pzLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.photograph){
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            path="/sdcard/DCIM/Camera/"+System.currentTimeMillis()+".jpg";
            Uri uriForFile = FileProvider.getUriForFile(this, "com.example.szh", new File(path));
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uriForFile);
            startActivityForResult(intent,111);
            pzLinear.setVisibility(View.GONE);
        }else if (v.getId()==R.id.map_depot){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent,555);
            pzLinear.setVisibility(View.GONE);
        }
    }

    @Override
    public View addStatusView() {
        return f(R.id.user_linear);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_user;
    }

    @Override
    public void initView() {
        userBackBnt = (ImageView) findViewById(R.id.user_back_bnt);
        userToUserImg = (ImageView) findViewById(R.id.user_toUserImg);
        userToUserImg.setOnClickListener(this);
        userToUserName = (TextView) findViewById(R.id.user_toUserName);
        userBackBnt.setOnClickListener(this);
        userToUserName.setOnClickListener(this);
        sex = (TextView) findViewById(R.id.sex);
        sex.setOnClickListener(this);
        sexLinear = (LinearLayout) findViewById(R.id.sex_linear);
        sexNull = (Button) findViewById(R.id.sex_null);
        sexNull.setOnClickListener(this);
        sexBody = (Button) findViewById(R.id.sex_body);
        sexBody.setOnClickListener(this);
        sexGirl = (Button) findViewById(R.id.sex_girl);
        sexGirl.setOnClickListener(this);
        cancel1 = (Button) findViewById(R.id.cancel1);
        cancel1.setOnClickListener(this);
        pzLinear = (LinearLayout) findViewById(R.id.pz_linear);
        photograph = (Button) findViewById(R.id.photograph);
        photograph.setOnClickListener(this);
        mapDepot = (Button) findViewById(R.id.map_depot);
        mapDepot.setOnClickListener(this);
        cancel2 = (Button) findViewById(R.id.cancel2);
        cancel2.setOnClickListener(this);
        userLinear = (LinearLayout) findViewById(R.id.user_linear);
    }

    @Override
    public void inject() {
        DaggerUpDataComponent.builder().upDataUserModues(new UpDataUserModues(this)).build().inject(this);
    }

    @Override
    public void initData() {
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.VIBRATE,
                    Manifest.permission.WAKE_LOCK
            },105);
        }
        userToUserImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pzLinear.setVisibility(View.VISIBLE);
            }
        });
        userBackBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                    LogUtils.json(list.get(0));
                    p.requestUpDataUser(list.get(0));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();
            }
        });
        try {
            List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
            userToUserName.setText(list.get(0).getUser_name());
            if (list.get(0).getUser_sex().equals("1")){
                sex.setText("男");
            }else {
                sex.setText("女");
            }
            LoadImage.loadImg(list.get(0).getUser_img(),userToUserImg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == 102) {
            String nikename = data.getStringExtra("nikename");
            userToUserName.setText(nikename);
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();

                if (list.size() != 0) {
                    list.get(0).setUser_name(nikename);
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));

            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (requestCode==111&&resultCode== Activity.RESULT_OK){
            LoadImage.loadImg(path,userToUserImg);
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                if (list.size() != 0) {
                    list.get(0).setUser_img(path);
                    list.get(0).setUser_integral(1);
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
            p.RequestUpload(path);
        }else if (requestCode==555&&resultCode== Activity.RESULT_OK){
            Uri data1 = data.getData();
            String s = data1.toString();
            LoadImage.loadImg(s,userToUserImg);
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                if (list.size() != 0) {
                    list.get(0).setUser_img(s);
                    list.get(0).setUser_integral(1);
                }
                DBManager.getInstance().getDaoSession().update(list.get(0));
                LogUtils.json(DBManager.getInstance().getDaoSession().loadAll(UserEntity.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
            p.RequestUpload("/sdcard/DCIM/Camera/~B255Y1D~1}D1CGT{Y({C}7.jpg");
        }
    }

    @Override
    public void refresh(BaseEntity baseEntity) {
//        if (baseEntity instanceof BaseEntity){
//            if (baseEntity.status.equals("200")){
//                ToastUtils.showMsg("更新成功");
//            }else {
//                ToastUtils.showMsg(baseEntity.message);
//            }
//        }else if (baseEntity instanceof UploadEntity){
//            UploadEntity entity = (UploadEntity) baseEntity;
//            if (entity.getStatuesCode().equals("200")){
//                ToastUtils.showMsg("上传成功");
//            }
//        }

    }
}